package com.server.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.server.common.QueryPageParam;
import com.server.common.Result;
import com.server.entity.Orders;
import com.server.entity.Unorder;
import com.server.service.GoodsService;
import com.server.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/order")
public class OrdersController {

	@Autowired
	private OrdersService ordersService;

	@Autowired
	private GoodsService goodsService;

	@PostMapping("/saveORmodify")
	public Result saveORmodify(@RequestBody Orders orders){
		System.out.println(orders);
		if(ordersService.saveOrUpdate(orders))
			return Result.suc(null, 1L);
		else
			return Result.fail();
	}
	@PostMapping("/delete")
	public Result delete(@RequestBody Map<String, List<Integer>> requestMap){
		boolean res = true;
		List<Integer> ids = requestMap.get("ids");
		for(Integer id : ids){
			if(id!=null)
				res=res& ordersService.removeById(id);
		}
		if(res)
			return Result.suc(null, 1L);
		else
			return Result.fail();
	}

	@PostMapping("/querylike")
	public Result querylike(@RequestBody QueryPageParam query) {
		// 创建分页对象
		Page<Orders> page = new Page<>(query.getPageNum(), query.getPageSize());

		// 获取查询参数
		HashMap<String, Object> map = query.getParam();

		// 创建条件构造器
		LambdaQueryWrapper<Orders> lambdaQueryWrapper = new LambdaQueryWrapper<>();

		// 动态拼接条件
		String id = map.get("id") != null ? map.get("id").toString().trim() : "";
		String uid = map.get("uid") != null ? map.get("uid").toString().trim() : "";
		String gid = map.get("gid") != null ? map.get("gid").toString().trim() : "";
		String time = map.get("time") != null ? map.get("time").toString().trim() : "";
		System.out.println(id+" "+uid+" "+gid+" "+time);
		if (!id.isEmpty()) {
				lambdaQueryWrapper.eq(Orders::getId, id);
		}
		if (!uid.isEmpty()) {
			lambdaQueryWrapper.like(Orders::getUid, uid);
		}
		if (!gid.isEmpty()) {
			lambdaQueryWrapper.like(Orders::getGid, gid); // 精确查询
		}
		if (!time.isEmpty()) {
			lambdaQueryWrapper.like(Orders::getTime, time); // 精确查询
		}
		// 执行查询
		IPage<Orders> res = ordersService.pageC(page, lambdaQueryWrapper);
		// 返回结果
		return Result.suc(res.getRecords(), res.getTotal());
	}
	@PostMapping("/userquerylike")
	public Result userquerylike(@RequestBody QueryPageParam query) {
		// 创建分页对象
		Page<Orders> page = new Page<>(query.getPageNum(), query.getPageSize());

		// 获取查询参数
		HashMap<String, Object> map = query.getParam();

		// 创建条件构造器
		LambdaQueryWrapper<Orders> lambdaQueryWrapper = new LambdaQueryWrapper<>();

		// 动态拼接条件
		String id = map.get("id") != null ? map.get("id").toString().trim() : "";
		String uid = map.get("uid") != null ? map.get("uid").toString().trim() : "";
		String gid = map.get("gid") != null ? map.get("gid").toString().trim() : "";
		String time = map.get("time") != null ? map.get("time").toString().trim() : "";
		System.out.println(id+" "+uid+" "+gid+" "+time);
		if (!id.isEmpty()) {
			lambdaQueryWrapper.eq(Orders::getId, id);
		}
		if (!uid.isEmpty()) {
			lambdaQueryWrapper.like(Orders::getUid, uid);
		}
		if (!gid.isEmpty()) {
			lambdaQueryWrapper.like(Orders::getGid, gid); // 精确查询
		}
		if (!time.isEmpty()) {
			lambdaQueryWrapper.like(Orders::getTime, time); // 精确查询
		}
		// 执行查询
		IPage<Orders> res = ordersService.pageC(page, lambdaQueryWrapper);
		List<Orders> dataList = res.getRecords();
		Map<String, String> nameList = new HashMap<>();
		for (Orders orders : dataList) {
			String gid1 = String.valueOf(orders.getGid()); // 获取商品ID
			String name = goodsService.getById(orders.getGid()).getName(); // 获取商品名称
			nameList.put(gid1, name); // 将商品ID和名称存入 Map 中
		}
		Map<String, Object> data = new HashMap<>();
		data.put("dataList", dataList);
		data.put("namemap", nameList);

		// 返回结果
		return Result.suc(data, res.getTotal());
	}
}
